#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# dialogs.py
#
#  Copyright (c) 2006 INdT (Instituto Nokia de Technologia)
#
#  Author: Marcelo Lira dos Santos <setanta@gmail.com>
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA

import pygtk
pygtk.require("2.0")
import gtk
import gobject
import icon_theme
import time
import sound_notification
import threading

from gettext import gettext as _

class AboutDialog(gtk.AboutDialog):

    def __init__(self):
        gtk.AboutDialog.__init__(self)

        self.set_name(_("Ereséva Messenger"))
        self.set_comments(_("GTalk Instant Messaging Client"))
        icon = icon_theme.icon_theme_get_default().load_icon("ereseva", 128, 0)
        self.set_logo(icon)
        self.set_authors(["Kenneth Rohde Christiansen <kenneth.christiansen@gmail.com>",
                            "Marcelo Lira dos Santos <setanta@gmail.com>"])
        self.set_artists(["Lapo Calamandrei <calamandrei@gmail.com>",
                            "Andreas Nilsson <nisses.mail@home.se>", "Hylke Bons"])
        self.set_copyright(_("Copyright © 2007 INdT and Kenneth Christiansen"))
        self.set_property("icon-name", 'ereseva')

        self.show()


class SignOutDialog(gtk.Dialog):

    def __init__(self, parent=None):
        gtk.Dialog.__init__(self, title=_("Really sign out?"), parent=parent, \
                                    flags=gtk.DIALOG_DESTROY_WITH_PARENT,
                                    buttons=( gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                    gtk.STOCK_DISCONNECT, gtk.RESPONSE_YES ))

        self.set_property("icon-name", 'ereseva')

        self.hbox1 = gtk.HBox(False, 0)

        self.image1 = gtk.image_new_from_stock(gtk.STOCK_DIALOG_QUESTION, \
                gtk.ICON_SIZE_DIALOG)
        self.image1.set_size_request(60, -1)

        self.hbox1.pack_start(self.image1, True, True, 6)

        self.label1 = gtk.Label()
        self.label1.set_size_request(-1, 100)
        self.label1.set_line_wrap(True)
        self.label1.set_markup(_("<big><b>If you sign out, all your conversation "\
                "windows will be closed.</b></big>\n\nSure you want to do that?"))

        self.hbox1.pack_start(self.label1, False, True, 6)

        self.vbox.pack_start(self.hbox1, True, True, 0)

        self.show_all()

class IncomingCallDialog(gtk.Window):

    (
    RESPONSE_IGNORE,
    RESPONSE_ANSWER
    ) = range(2)

    __gsignals__ = {
        'user-response' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                (gobject.TYPE_PYOBJECT,)),
    }

    def __init__(self, name, parent=None):
        gtk.Window.__init__(self)#, parent=parent, flags=gtk.DIALOG_DESTROY_WITH_PARENT)

        self.set_title(_("Incoming Call..."))

        self.set_property("icon-name", 'ereseva')
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_resizable(False)

        self.signal_emited = False

        self.connect('destroy', self.on_reponse,\
                IncomingCallDialog.RESPONSE_IGNORE)

        self.vbox = gtk.VBox()
        self.vbox.set_border_width(6)
        self.add(self.vbox)

        itheme = icon_theme.icon_theme_get_default()

        pick_up_image = gtk.Image()
        pick_up_image.set_from_pixbuf(itheme.load_icon("phone-hang-up", 22, 0))
        hang_up_image = gtk.Image()
        hang_up_image.set_from_pixbuf(itheme.load_icon("phone-pick-up", 22, 0))

        phone_image = gtk.Image()
        phone_image.set_from_pixbuf(itheme.load_icon("start-voip-session", 48, 0))

        self.hbox = gtk.HBox()
        self.hbox.set_border_width(6)
        self.hbox.set_spacing(12)

        recv_time = time.strftime(_("%I:%M %p on %A"), time.localtime())
        label = gtk.Label()
        label.set_line_wrap(True)
        label.set_markup(_("<big><b>Incoming call from %s at %s. "\
                "Please answer or ignore.</b></big>") % \
                (name, recv_time))

        self.hbox.pack_start(phone_image)
        self.hbox.pack_start(label)

        self.vbox.pack_start(self.hbox)

        deny_button = gtk.Button()
        deny_button.set_label(_("Ignore"))
        deny_button.set_image(pick_up_image)
        deny_button.connect('clicked', self.on_reponse,\
                IncomingCallDialog.RESPONSE_IGNORE)

        accept_button = gtk.Button()
        accept_button.set_label(_("Answer"))
        accept_button.set_image(hang_up_image)
        accept_button.connect('clicked', self.on_reponse,\
                IncomingCallDialog.RESPONSE_ANSWER)

        self.hbuttonbox1 = gtk.HButtonBox()
        self.hbuttonbox1.set_border_width(6)
        self.hbuttonbox1.set_layout(gtk.BUTTONBOX_END)
        self.hbuttonbox1.set_spacing(6)
        self.hbuttonbox1.add(deny_button)
        self.hbuttonbox1.add(accept_button)

        self.vbox.pack_start(self.hbuttonbox1, False, True, 0)

        #self.run_ = True

        #def start_ringing():
        #    while self.run_:
        #        sound_notification.play_sound("ring-tone-1.wav")
        #    print "finished"

        #self.thread = threading.Thread(target=start_ringing, name="RingThread")

        def stop_ringing(dialog, responce_id):
            self.run_ = False

        #self.connect("response", stop_ringing)
        #self.thread.start()

        self.show_all()

    def on_reponse(self, widget, response):
        if not self.signal_emited:
            self.signal_emited = True
            self.emit('user-response', response)


class OutgoingCallDialog(gtk.Window):

    __gsignals__ = {
        'call-canceled' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                []),
    }

    def __init__(self, name, parent=None): 
        gtk.Window.__init__(self)#, parent=parent, flags=gtk.DIALOG_DESTROY_WITH_PARENT)

        self.set_title('Outgoing Call...')

        self.set_property("icon-name", 'ereseva')
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_resizable(False)

        #self.connect('destroy', self.on_reponse)
        print self.on_response(None)

        self.vbox = gtk.VBox()
        self.vbox.set_border_width(6)
        self.add(self.vbox)

        itheme = icon_theme.icon_theme_get_default()

        hang_up_image = gtk.Image()
        hang_up_image.set_from_pixbuf(itheme.load_icon("phone-hang-up", 22, 0))

        phone_image = gtk.Image()
        phone_image.set_from_pixbuf(itheme.load_icon("start-voip-session", 48, 0))

        self.hbox = gtk.HBox()
        self.hbox.set_border_width(6)
        self.hbox.set_spacing(12)

        recv_time = time.strftime(_("%I:%M %p on %A"), time.localtime())
        label = gtk.Label()
        label.set_line_wrap(True)
        label.set_markup(_("<big><b>Outgoing call to %s at %s. "\
                "Please wait or cancel the call.</b></big>") % \
                (name, recv_time))

        self.hbox.pack_start(phone_image)
        self.hbox.pack_start(label)

        self.vbox.pack_start(self.hbox)

        cancel_button = gtk.Button()
        cancel_button.set_label(_("Cancel"))
        cancel_button.set_image(hang_up_image)
        #cancel_button.connect('clicked', self.on_reponse)

        self.hbuttonbox1 = gtk.HButtonBox()
        self.hbuttonbox1.set_border_width(6)
        self.hbuttonbox1.set_layout(gtk.BUTTONBOX_END)
        self.hbuttonbox1.set_spacing(6)
        self.hbuttonbox1.add(cancel_button)

        self.vbox.pack_start(self.hbuttonbox1)

        #def start_ringing():
        #    while self.run_:
        #        sound_notification.play_sound("ring-tone-1.wav")
        #    print "finished"

        #self.thread = threading.Thread(target=start_ringing, name="RingThread")

        #def stop_ringing(dialog, responce_id):
        #    self.run_ = False

        #self.connect("response", stop_ringing)
        #self.thread.start()

        self.show_all()


    def on_response(self, widget, data=None):
        self.emit('call-canceled')


class ConnectionProblemDialog(gtk.Dialog):

    def __init__(self, parent=None):
        gtk.Dialog.__init__(self, title=_("Connection Problem"), parent=parent, \
                                    flags=gtk.DIALOG_DESTROY_WITH_PARENT,
                                    buttons=( gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                    gtk.STOCK_REFRESH, gtk.RESPONSE_YES))

        self.set_property("icon-name", 'ereseva')

        self.hbox1 = gtk.HBox(False, 0)

        self.image1 = gtk.image_new_from_stock(gtk.STOCK_DIALOG_ERROR, \
                gtk.ICON_SIZE_DIALOG)
        self.image1.set_size_request(60, -1)

        self.hbox1.pack_start(self.image1, True, True, 6)

        self.label1 = gtk.Label()
        self.label1.set_size_request(-1, 100)
        self.label1.set_line_wrap(True)
        self.label1.set_markup(_("<big><b>There was a problem when trying to connect "\
                "to your account.</b></big>\n\nDo you want to try connecting again?"))

        self.hbox1.pack_start(self.label1, False, True, 6)

        self.vbox.pack_start(self.hbox1, True, True, 0)

        self.show_all()
